package main

import (
	"encoding/json"
	"fmt"
)

type mperson struct {
	Name string `json:"name" db:"name"`
	Age  int    `json:"age" db:"age"`
}

func main() {
	p := mperson{
		"robbin",
		18,
	}

	//序列化结构体->JSON （结构体字段要大写）
	b, err := json.Marshal(p)
	if err != nil {
		fmt.Printf("marshal failed, err:%v", err)
	}
	fmt.Printf("%#v\n", string(b))

	// 反序列化
	str := `{"name":"robbin","age":18}`
	var p2 mperson
	json.Unmarshal([]byte(str), &p2)
	fmt.Printf("%#v\n", p2)
}
